package cn.calm.template.action;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.osgi.interceptor.ServiceAware;

import cn.calm.commons.action.CurdAction;
import cn.calm.commons.entity.PageMode;
import cn.calm.entity.Template;
import cn.calm.template.service.ITemplateService;

@SuppressWarnings("rawtypes")
public class TemplateAction<T extends Template,S extends ITemplateService<T>> extends
CurdAction<T,S> implements ServiceAware<ITemplateService>  {

	/**
	 * 
	 */
	
	private static final long serialVersionUID = 1L;
	
	private static final Logger logger=Logger.getLogger(TemplateAction.class);
	
	private String dependencies;
	public String loadDependencies(){
		logger.debug("loadDependencies start");
		setList(service.listInByProperty(getEntityClass(),"id",dependencies.split(",")));
		logger.debug("loadDependencies end");
		return "loadDependencies";
	}
	@Override
	public String listPage() {
		logger.debug("listPage start");
		pageMode=service.listPage(pageMode, t,"id");
		logger.debug("listPage end");
		return "listPage";
	}
	@Override
	public List<T> getList() {
		return super.getList();
	}

	@Override
	public Map<String, List<String>> getFieldErrors() {
		return super.getFieldErrors();
	}

	@Override
	public Collection<String> getActionErrors() {
		return super.getActionErrors();
	}

	@Override
	public T getT() {
		return super.getT();
	}

	@Override
	public PageMode getPageMode() {
		return super.getPageMode();
	}

	public String getDependencies() {
		return dependencies;
	}

	public void setDependencies(String dependencies) {
		this.dependencies = dependencies;
	}

	@Override
	public String getModeName() {
		return "template";
	}
	@SuppressWarnings("unchecked")
	@Override
	public void setServices(List<ITemplateService> services) {
		if (service == null) {
			if (services != null && services.size() > 0) {
				service = (S) services.get(0);
			}
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public Class<T> getEntityClass() {
		return (Class<T>) Template.class;
	}
}
